@using System.Reflection
@using Elsa.Studio.Contracts
@using Elsa.Studio.Shell.Options
@using Microsoft.Extensions.Options

<Router AppAssembly="@typeof(App).Assembly" AdditionalAssemblies="@AdditionalAssemblies">
    <Found Context="routeData">
        @if (!AuthorizationIsDisabled)
        {
            <CascadingAuthenticationState>
                <AuthorizeRouteView RouteData="routeData" DefaultLayout="@typeof(MainLayout)">
                    <Authorizing>
                        <Well>
                            <MudText Typo="Typo.body2">Authorizing</MudText>
                        </Well>
                    </Authorizing>
                    <NotAuthorized>
                        @UnauthorizedComponentProvider.GetUnauthorizedComponent()
                    </NotAuthorized>
                </AuthorizeRouteView>
            </CascadingAuthenticationState>
        }
        else
        {
            <RouteView RouteData="routeData" DefaultLayout="@typeof(MainLayout)">
            </RouteView>
        }
        <FocusOnNavigate RouteData="@routeData" Selector="h1"/>
    </Found>
    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(MainLayout)">
            <p role="alert">Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>

@code {
    
    [Inject]
    private IEnumerable<IFeature> Modules { get; set; } = default!;

    [Inject]private IUnauthorizedComponentProvider UnauthorizedComponentProvider { get; set; } = default!;
    
    [Inject] private IOptions<ShellOptions> ShellOptions { get; set; } = default!;

    private IEnumerable<Assembly> AdditionalAssemblies { get; set; } = default!;
    
    private bool AuthorizationIsDisabled => ShellOptions.Value.DisableAuthorization;

    /// <inheritdoc />
    protected override void OnInitialized()
    {
        AdditionalAssemblies = Modules.Select(x => x.GetType().Assembly).Distinct().ToList();
    }

}